import json  # noqa

from django.http import HttpRequest, JsonResponse
from pinject import new_object_graph

from common.auth_utils import login_required
from common.errors import InvalidParameter
from common.http_utils import SuccessResponse
from common.request_utils import get_request_data, request_parameter_check
from user_center import forms
from user_center.application.service import UserService
from user_center.forms import (
    CampusIdentityForm,
    UpdateEmailForm,
    UserInfoForm,
    VerificationCodeForm,
)
from user_center.infrastructure import adapter, repository


def login(request: HttpRequest) -> JsonResponse:
    form = request_parameter_check(
        get_request_data(request), form_class=forms.LoginForm
    )
    if (not form.cleaned_data["password"]) + (not form.cleaned_data["code"]) != 1:
        raise InvalidParameter("必须提供密码或验证码之一！")
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    token = service.login(
        email=form.cleaned_data["email"],
        password=form.cleaned_data["password"],
        email_verification_code=form.cleaned_data["code"],
    )
    return SuccessResponse({"message": "注册成功！", "token": token})


def logout(request: HttpRequest) -> JsonResponse:
    return None


def register(request: HttpRequest) -> JsonResponse:
    form = request_parameter_check(
        get_request_data(request), form_class=forms.RegisterForm
    )
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.register(
        username=form.cleaned_data["username"],
        password=form.cleaned_data["password"],
        email=form.cleaned_data["email"],
        code=form.cleaned_data["code"],
    )
    return SuccessResponse(content={"message": "注册成功！"})


@login_required
def get_user_info(request: HttpRequest) -> JsonResponse:
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    user_info = service.get_user_info(user_id=request.user.id)
    return SuccessResponse({"user_info": user_info})


@login_required
def update_user_info(request: HttpRequest) -> JsonResponse:
    form = request_parameter_check(get_request_data(request), form_class=UserInfoForm)
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.update_user_info_by_id(id=request.user.id, **form.cleaned_data)
    return SuccessResponse({"message": "修改成功！"})


def auth_campus_identity(request: HttpRequest) -> JsonResponse:
    return None


def send_email_verification_code_4_register(request: HttpRequest) -> JsonResponse:
    form = request_parameter_check(
        get_request_data(request), form_class=VerificationCodeForm
    )
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.send_email_verification_code_4_register(email=form.cleaned_data["email"])
    return SuccessResponse(content={"message": "验证码发送成功，请您注意查收！"})


def send_email_verification_code_4_login(request):
    form = request_parameter_check(
        get_request_data(request), form_class=VerificationCodeForm
    )
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.send_email_verification_code_4_login(email=form.cleaned_data["email"])
    return SuccessResponse(content={"message": "验证码发送成功，请您注意查收！"})


@login_required
def update_email(request):
    form = request_parameter_check(
        get_request_data(request), form_class=UpdateEmailForm
    )
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.update_email_by_id(id=request.user.id, **form.cleaned_data)
    return SuccessResponse({"message": "邮箱修改成功！"})


def send_email_verification_code_4_change_email(request):
    form = request_parameter_check(
        get_request_data(request), form_class=VerificationCodeForm
    )
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.send_email_verification_code_4_change_email(
        email=form.cleaned_data["email"]
    )
    return SuccessResponse(content={"message": "验证码发送成功，请您注意查收！"})


@login_required
def update_campus_identity(request):
    form = request_parameter_check(
        get_request_data(request), form_class=CampusIdentityForm
    )
    obj_graph = new_object_graph(modules=[adapter, repository])
    service = obj_graph.provide(UserService)
    service.update_campus_identity(id=request.user.id, **form.cleaned_data)
    return SuccessResponse(content={"message": "验证码发送成功，请您注意查收！"})
